package com.sayyi.demo.retry;

import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class RetryService {

    /**
     * 通过外部调用时，才能够触发重试
     * @return
     */
    @Retryable(value = RuntimeException.class, recover = "recover", backoff = @Backoff(delay = 100))
    public String invoke() {
        return retryAble();
    }

    public String retryAble() {
        log.info("retryAble invoked");
        throw new RuntimeException("调用异常");
    }

    @Recover
    public String recover(RuntimeException e) {
        log.info("recover invoked, exception is {}", e.getMessage());
        return "recovered";
    }
}
